package com.example.user.controller;

import com.example.common.result.Result;
import com.example.user.api.UserDTO;
import com.example.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 用户类
 * CURD 部分
 */
@RestController
@RequestMapping("/api/users")
@ApiOperation("用户控制器")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 创建用户
     */
    @PostMapping
    @ApiOperation("创建用户")
    public Result<String> createUser(@RequestBody UserDTO userDTO)
    {
        boolean usercreate = userService.createUser(userDTO); //创建逻辑

        //构建响应部分
        String userinfo = userDTO.getUsername();
        return Result.success(userinfo);
    }

    /**
     * 更新用户 包括UserDto中包含的各部分字段信息
     * user id不能未空 从token中获取
     */
    @PutMapping
    @ApiOperation("更新用户")
    public Result<Boolean> updateUser(@RequestBody UserDTO userDTO)  // @Valid 表示参数校验userDto对应信息
    {
        boolean success = userService.updateUser(userDTO);
        return Result.success(success);
    }

    /**
     * 删除用户
     */
    //硬删除
    @DeleteMapping("/{userId}")
    @ApiOperation("删除用户")
    public Result<Boolean> deleteUser(@PathVariable Long userId) {
        boolean success = userService.deleteUser(userId);
        return Result.success(success);
    }
    //软删除
//    @DeleteMapping("/soft/{userId}")
//    @ApiOperation("软删除用户")
//    public Result<Boolean> softdeleteUser(@PathVariable Long userId) {
//        boolean success = userService.softdeleteUser(userId);
//        return Result.success(success);
//    }

    /**
     * 根据ID查询用户
     */
    @GetMapping("/id/{userId}")
    @ApiOperation("根据ID查询用户")
    public Result<UserDTO> getUserById(@PathVariable Long userId) {
        UserDTO userDTO = userService.getUserById(userId);
        return Result.success(userDTO);
    }
    /**
     * 根据name查询用户 同getUserById的url
     */
    @GetMapping("/name/{username}")
    @ApiOperation("根据name查询用户")
    public Result<UserDTO> getUserByName(@PathVariable String username) {
        UserDTO userDTO = userService.getUserByName(username);
        return Result.success(userDTO);
    }

    /**
     * 查询所有用户
     */
    @GetMapping
    @ApiOperation("查询所有用户")
    public Result<List<UserDTO>> listUsers() {
        List<UserDTO> userList = userService.listUsers();
        return Result.success(userList);
    }


    //扩展接口



}
